package com.mall507.login.interceptor;

import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.core.BaseSP;
import com.mall507.comn.tools.UserTools;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录拦截器
 *
 * @author hzh
 * @version 1.0
 * @date 2018/4/9
 */
@Interceptor(priority = 1,name = "LoginIntercept")
public class LoginInterceptor implements IInterceptor {
    
    public Map<String,String> interceptorMap;
    
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if(null==(interceptorMap.get(postcard.getPath()))){
            //不需要登录拦截的路由 
            callback.onContinue(postcard);
        }else{
            if (UserTools.getInstance().getIsLogin()) {
                //已登录
                callback.onContinue(postcard);
            } else {
                //未登录
                callback.onInterrupt(null);
                Bundle bundle = postcard.getExtras();
                bundle.putString("targetPath", postcard.getPath());
                ARouter.getInstance().build("/login/login").with(bundle).greenChannel().navigation();
            }
        }
    }

    @Override
    public void init(Context context) {
        //添加需要拦截的路由页面
        interceptorMap=new HashMap<String,String>();
        interceptorMap.put("/user/UserRedAwardRecordUI","/user/UserRedAwardRecordUI");

        interceptorMap.put("/find/FindSubmitReleaseUI","/find/FindSubmitReleaseUI");
        interceptorMap.put("/find/FindLostWriteUI","/find/FindLostWriteUI");

        interceptorMap.put("/community/CommunityProblemReleaseUI","/community/CommunityProblemReleaseUI");
        
        interceptorMap.put("/mine/SettingUI","/mine/SettingUI");
        interceptorMap.put("/mine/FeedBackUI","/mine/FeedBackUI");
        interceptorMap.put("/mine/MessageUI","/mine/MessageUI");
        interceptorMap.put("/mine/CommentRUI","/mine/CommentRUI");
        interceptorMap.put("/mine/ReleaseUI","/mine/ReleaseUI");
        interceptorMap.put("/mine/ProblemRUI","/mine/ProblemRUI");

        interceptorMap.put("/community/CommunityBusinessEnterUI","/community/CommunityBusinessEnterUI");
        interceptorMap.put("/community/CommunityAssociationEnterUI","/community/CommunityAssociationEnterUI");
    }
}
